package DAOIMPL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBConnection;

import VO.EMPBEAN;
import DAO.EMPDAO;

public class EMPimpl implements EMPDAO {
	public EMPBEAN check(String name) {
		Connection con = null;
		EMPBEAN empBean = null;
		PreparedStatement pstmtCheck = null;
		ResultSet rsCheck = null;
		try {
			con = DBConnection.getConnection();
			pstmtCheck = con.prepareStatement("select * from emp where name=?");
			pstmtCheck.setString(1, name);
			rsCheck = pstmtCheck.executeQuery();
			while (rsCheck.next()) {
				String namec = rsCheck.getString("name");
				String sax = rsCheck.getString("sax");
				String age = rsCheck.getString("age");
				String sal = rsCheck.getString("sal");
				empBean = new EMPBEAN(namec, sax, age, sal);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return empBean;
	}

	public void delete(String name) {
		// �������ɾ��
		Connection con = null;
		PreparedStatement pstmtDelete = null;
		con = DBConnection.getConnection();
		try {
			pstmtDelete = con.prepareStatement("delete from emp where name=?");
			pstmtDelete.setString(1, name);
			pstmtDelete.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			if (con != null) {
				try {
					con.close();
				} catch (SQLException sql) {
					sql.printStackTrace();
				}
			}
		}

	}

	public void insert(EMPBEAN empbean) {
		// ������
		Connection con = null;
		String name = empbean.getName();
		String age = empbean.getAge();
		String sax = empbean.getSax();
		String sal = empbean.getSal();
		String insertSql = "insert into emp values(?,?,?,?)";
		PreparedStatement pstmtInsert = null;
		con = DBConnection.getConnection();
		try {
			pstmtInsert = con.prepareStatement(insertSql);
			pstmtInsert.setString(1, name);
			pstmtInsert.setString(2, sax);
			pstmtInsert.setString(3, age);
			pstmtInsert.setString(4, sal);
			pstmtInsert.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException sql) {
					sql.printStackTrace();
				}
			}
		}

	}

	public List<EMPBEAN> select() {
		List<EMPBEAN> list = new ArrayList<EMPBEAN>();
		PreparedStatement pstmtSelect = null;
		ResultSet rs = null;
		Connection con = null;
		con = DBConnection.getConnection();
		try {
			pstmtSelect = con.prepareStatement("select * from emp");
			rs = pstmtSelect.executeQuery();
		} catch (SQLException sql) {
			sql.printStackTrace();
		}
		try {
			while (rs.next()) {
				EMPBEAN user = new EMPBEAN();
				user.setName(rs.getString(1));
				user.setAge(rs.getString(2));
				user.setSax(rs.getString(3));
				user.setSal(rs.getString(4));
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	public void update(EMPBEAN empBean) {
		String name = empBean.getName();
		String age = empBean.getAge();
		String sax = empBean.getSax();
		String sal = empBean.getSal();
		Connection con = null;
		String upsql = "update emp set sax=?,set age=?, set sal=? where name=?";
		PreparedStatement pstmtUpdate = null;
		con = DBConnection.getConnection();
		try {
			pstmtUpdate = con.prepareStatement(upsql);
			pstmtUpdate.setString(1, sax);
			pstmtUpdate.setString(2, age);
			pstmtUpdate.setString(3, sal);
			pstmtUpdate.setString(4, name);
			pstmtUpdate.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
//	public static void main(String[] args) {
//		EMPBEAN bean=new EMPBEAN("11","22","33","44");
//		EMPimpl te=new EMPimpl();
//		te.update(bean);
//		System.out.print(bean.getName()+bean.getSax()+bean.getAge()+bean.getSal());
//	}
	

}
